package com.baichuan.zntx.service.impl;

import com.baichuan.common.core.utils.MapstructUtils;
import com.baichuan.common.core.utils.StringUtils;
import com.baichuan.common.mybatis.core.page.TableDataInfo;
import com.baichuan.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baichuan.zntx.domain.bo.RfidBo;
import com.baichuan.zntx.domain.vo.RfidVo;
import com.baichuan.zntx.domain.Rfid;
import com.baichuan.zntx.mapper.RfidMapper;
import com.baichuan.zntx.service.IRfidService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * RFID标签管理Service业务层处理
 *
 * @author Baichuan
 * @date 2024-05-11
 */
@RequiredArgsConstructor
@Service
public class RfidServiceImpl implements IRfidService {

    private final RfidMapper baseMapper;

    /**
     * 查询RFID标签管理
     */
    @Override
    public RfidVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询RFID标签管理列表
     */
    @Override
    public TableDataInfo<RfidVo> queryPageList(RfidBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Rfid> lqw = buildQueryWrapper(bo);
        Page<RfidVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询RFID标签管理列表
     */
    @Override
    public List<RfidVo> queryList(RfidBo bo) {
        LambdaQueryWrapper<Rfid> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Rfid> buildQueryWrapper(RfidBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Rfid> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRfidCode()), Rfid::getRfidCode, bo.getRfidCode());
        return lqw;
    }

    /**
     * 新增RFID标签管理
     */
    @Override
    public Boolean insertByBo(RfidBo bo) {
        Rfid add = MapstructUtils.convert(bo, Rfid.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改RFID标签管理
     */
    @Override
    public Boolean updateByBo(RfidBo bo) {
        Rfid update = MapstructUtils.convert(bo, Rfid.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Rfid entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除RFID标签管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
